// views/index/shop/certificate/paypage/index.js
var BASE_URL = getApp().globalData.BASE_URL
var area = require('../../../../../utils/area');
var util = require('../../../../../utils/util');
import Notify from '../../../../../miniprogram_npm/@vant/weapp/notify/notify';
import Dialog from '../../../../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _num: 1,
    areaList: area.default,
    area: '',
    jiajipricelast:0,
    showarea: false, //地区遮罩
    showkuaidipop: false, //快递须知
    showfanyipop: false, //翻译须知
    showjiajipop: false, //加急须知
    showsaomiaopop: false, //扫描须知
    userid: '', //用户id
    option1: [{
        text: '顺丰到付(顺丰标快)',
        value: '顺丰到付(顺丰标快)'
      },
      {
        text: '顺丰标快(23元)',
        value: '顺丰标快(23元)'
      },
      {
        text: '顺丰特惠(18元)',
        value: '顺丰特惠(18元)'
      },
      {
        text: 'DHL海外(出件后按重量计费)',
        value: 'DHL海外(出件后按重量计费)'
      },
      {
        text: '出件后联系',
        value: '出件后联系'
      },
    ],
    kuaidi_type: '顺丰到付(顺丰标快)', //快递下拉菜单
    kuaidi_price: 0,
    applyname: '', //申请人姓名
    applyname_pinyin: '', //申请人拼音
    applyphone: '', //申请人手机号
    applywechat: '', //申请人微信号
    attnname: '', //收件人名字
    attnphone: '', //收件人手机号
    attnaddress: '', //收件人地址
    attnemail: '', //收件人邮箱
    schoolname: '', //学校名字
    majorname: '', //专业名字
    remarks: "", //备注
    checked: true,
    last_price: '', //最后总价
    shoppingcardata: '', //购物车数据
    onfanyiprice: 0, //是否选中
    saomiaoprice: 0, //扫描价钱
    jiajiprice: 0, //加急价钱
    jz: 0,
    id: "", //收获地址id
    fanyibtn: false,
    jiajibtn: false,
    saomiaobth: false,
    showerweima: false,
    dizhilist: '',
    erweima: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  onShow: function () {
    var _this = this
    if (util.iflogin()) {
      var userinfo = wx.getStorageSync("USER")
      this.setData({
        userid: userinfo.id
      })
      _this.getshoppingdata()
      _this.getdizhilist()
      var _this = this

      wx.request({
        url: `${BASE_URL}/ajax/xcxwechatbangding`,
        method: "POST",
        data: {
          id: userinfo.id
        },
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          _this.setData({
            erweima: res.data
          })
        }
      })
    }
    var userinfo = wx.getStorageSync("USER")
    this.setData({
      attnemail: userinfo.email
    })
  },
  //选择快递
  select_kuaidi: function (e) {
    console.log(e)
    this.setData({
      kuaidi_type: e.detail
    })
    if (e.detail == '顺丰标快(23元)') {
      this.setData({
        kuaidi_price: 23
      })
    } else if (e.detail == '顺丰特惠(18元)') {
      this.setData({
        kuaidi_price: 18
      })
    } else {
      this.setData({
        kuaidi_price: 0
      })
    }
    this.getshoppingdata()
  },



  //获取购物车数据
  getshoppingdata: function () {
    var _this = this
    wx.request({
      url: `${BASE_URL}/gongzheng/gouwuche_list`,
      method: "POST",
      data: {
        pid: _this.data.userid,
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        if (res.data.status == 0) {
          Dialog.alert({
            title: '提示',
            message: '购物车为空,请添加商品',
          }).then(() => {
            wx.navigateBack({
              delta: 1
            })
          });
        }
        var str = []
        if (res.data.status !== 0) {
          res.data.info.forEach((item, index) => {
              str.push(item.subtotal)
          })
          var arr = str.reduce(function (a, b) {
            return a + b;
          });
          if (res.data.jiazuo !== 0) {
            arr = arr + 60
            _this.setData({
              jz: 60
            })
          } else {
            _this.setData({
              jz: 0
            })
          }
          if (_this.data.jiajibtn) {
            arr = arr + 120 * res.data.num
          }
          if (_this.data.saomiaobth) {
            arr = arr + 30
          }
          if (_this.data.kuaidi_type == '顺丰标快(23元)') {
            arr = arr + 23
          }
          if (_this.data.kuaidi_type == '顺丰特惠(18元)') {
            arr = arr + 18
          }
          _this.setData({
            shoppingcardata: res.data,
            last_price: arr
          })
        } else {
          _this.setData({
            shoppingcardata: '',
            last_price: 0
          })
        }

      }
    })

  },
  //获得地址
  getdizhilist: function () {
    var _this = this
    wx.request({
      url: `${BASE_URL}/gongzheng/dizhi`,
      method: "GET",
      data: {
        id: _this.data.userid
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {

        _this.setData({
          dizhilist: res.data
        })
      }
    })
  },
  //删除公证
  delgongzheng: function (e) {
    var _this = this
    console.log(e)
    wx.showModal({
      title: '提示',
      content: '确定删除此订单吗?',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: `${BASE_URL}/gongzheng/gz_del.html`,
            method: "GET",
            data: {
              id: e.currentTarget.dataset.id
            },
            header: {
              'Content-Type': 'application/json' //GET方式是这个
            },
            success(res) {

              _this.getshoppingdata()
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //购物车清空
  clearcardata: function () {
    var _this = this
    wx.showModal({
      title: '提示',
      content: '确定要清空购物车吗?',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: `${BASE_URL}/gongzheng/listqingkong`,
            method: "GET",
            data: {
              id: _this.data.userid
            },
            header: {
              'Content-Type': 'application/json' //GET方式是这个
            },
            success(res) {

              _this.getshoppingdata()
            }
          })
        } else if (res.cancel) {}
      }
    })
  },
  //删除地址
  deldizhi: function (e) {
    var _this = this
    wx.showModal({
      title: '提示',
      content: '确定删除此地址吗?',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: `${BASE_URL}/gongzheng/deletedizhi`,
            method: "GET",
            data: {
              id: e.currentTarget.dataset.id
            },
            header: {
              'Content-Type': 'application/json' //GET方式是这个
            },
            success(res) {

              _this.getdizhilist()
            }
          })
        } else if (res.cancel) {}
      }
    })

  },
  //使用地址
  userdizhi: function (e) {
    var _this = this
    var dizhi = _this.data.dizhilist[e.currentTarget.dataset.index]
    console.log(dizhi)
    this.setData({
      applyname: dizhi.sname, //申请人姓名
      applyname_pinyin: dizhi.pingyin, //申请人拼音
      applyphone: dizhi.tel, //申请人手机号
      applywechat: dizhi.wechat, //申请人微信号
      attnname: dizhi.name, //收件人名字
      attnphone: dizhi.dianhua, //收件人手机号
      attnaddress: dizhi.dizhi, //收件人地址
      attnemail: dizhi.email, //收件人邮箱
      schoolname: dizhi.waiyu, //学校名字
      majorname: dizhi.zhuanye, //专业名字
      remarks: dizhi.beizhu, //备注
      id: e.currentTarget.dataset.id, //收获地址id
      area: dizhi.qvyu
    })
  },

  //填写信息的下一步按钮
  first_next_one_bu: function () {
    var _this = this
    if (!_this.ifhanzi(_this.data.applyname)) {
      Notify('姓名输入错误');
    }
    if (!_this.ifhanzi(_this.data.attnname)) {
      Notify('姓名输入错误');
    }
    if (!_this.ifwewhat(_this.data.applywechat)) {
      Notify('微信号输入错误');
    }
    if (!_this.codeVerification(_this.data.applyphone)) {
      Notify('请输入正确的手机号');
    }
    if (!_this.codeVerification(_this.data.attnphone)) {
      Notify('请输入正确的手机号');
    }
    if (!_this.ifenglish(_this.data.applyname_pinyin)) {
      Notify('拼音输入错误');
    }
    if (!_this.ifemail(_this.data.attnemail)) {
      Notify('邮箱格式错误');
    }
    if (_this.data.attnaddress == "") {
      Notify('详细地址不能为空');
    }
    var str = []
    var str_price = []
    _this.data.shoppingcardata.info.forEach(item => {
      str.push(item.biyezheng)
      if (item.jiazuo == 1) {
        str_price.push(item.fu * 25)
      } else {
        str_price.push(item.fu * 25 + item.language)
      }
    })
    console.log(_this.data.userid, str, _this.data.applyname, _this.data.applyname_pinyin, _this.data.applyphone, _this.data.applywechat, _this.data.attnname, _this.data.attnphone, _this.data.area, _this.data.attnaddress, _this.data.kuaidi_price, _this.data.onfanyiprice, _this.data.jiajiprice, _this.data.saomiaoprice, _this.data.schoolname, _this.data.majorname, _this.data.attnemail, _this.data.remarks, _this.data.jz, str_price)
    if (_this.ifhanzi(_this.data.applyname) && _this.ifhanzi(_this.data.attnname) &&
      _this.ifwewhat(_this.data.applywechat) && _this.codeVerification(_this.data.applyphone) && _this.ifenglish(_this.data.applyname_pinyin) && _this.codeVerification(_this.data.attnphone) && _this.ifenglish(_this.data.applyname_pinyin) && _this.ifemail(_this.data.attnemail) && _this.data.attnaddress !== ""
    ) {
      _this.setData({
        showerweima: true
      })

    }
  },
  //提交下单
  closeandxiadanpop: function () {
    var _this = this
    var str = []
    var str_price = []
    _this.data.shoppingcardata.info.forEach(item => {
      str.push(item.biyezheng)
      if (item.jiazuo == 1) {
        str_price.push(item.fu * 25+item.xue)
      } else {
        str_price.push(item.fu * 25 + item.language+item.xue)
      }
    })
    wx.request({
      url: `${BASE_URL}/gongzheng/dingdan.html`,
      method: "POST",
      data: {
        pid: _this.data.userid,
        biyezheng: str,
        subtotal: str_price,
        sname: _this.data.applyname,
        pingyin: _this.data.applyname_pinyin,
        tel: _this.data.applyphone,
        wechat: _this.data.applywechat,
        name: _this.data.attnname,
        dianhua: _this.data.attnphone,
        qvyu: _this.data.area,
        dizhi: _this.data.attnaddress,
        kuaidi: _this.data.kuaidi_price,
        kdname: _this.data.kuaidi_type,
        translate: _this.data.onfanyiprice,
        urgent: _this.data.jiajiprice,
        scan: _this.data.saomiaoprice,
        waiyu: _this.data.schoolname,
        zhuanye: _this.data.majorname,
        beizhu: _this.data.remarks,
        email: _this.data.attnemail,
        id: _this.data.id,
        jz: _this.data.jz,
        type: 'xiaochengxv'
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        console.log(res)
        _this.setData({
          showerweima: false
        })
        wx.navigateTo({
          url: `./second/index?id=${res.data}`
        })
        _this.getdizhilist()
      }
    })
  },



  //手机号正则
  codeVerification: function (phone) {
    let phoneCodeVerification = /^[1][3,4,5,7,8][0-9]{9}$/;
    return phoneCodeVerification.test(phone);
  },
  //英文字母正则
  ifenglish: function (pinyin) {
    let ifenglish = /^[A-Za-z]+$/;
    return ifenglish.test(pinyin);
  },
  //汉字正则
  ifhanzi: function (pinyin) {
    let ifhanzi = /^[\u4e00-\u9fa5]+$/;
    return ifhanzi.test(pinyin);
  },
  //微信号正则
  ifwewhat: function (pinyin) {
    let ifwewhat = /^[0-9a-zA-Z]*$/g;
    return ifwewhat.test(pinyin);
  },
  //邮箱验证
  ifemail: function (email) {
    let ifemail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    return ifemail.test(email);
  },









  //附加服务按钮
  onfanyibtn: function (e) {
    this.setData({
      fanyibtn: e.detail
    })
    if (e.detail) {
      this.setData({
        onfanyiprice: 1,
      })
    } else {
      this.setData({
        onfanyiprice: 0,
      })
    }
  },
  onjiajibtn: function (e) {
    var _this = this
    this.setData({
      jiajibtn: e.detail
    })
    if (e.detail) {
      this.setData({
        jiajipricelast: 120 * _this.data.shoppingcardata.num,
        jiajiprice:120,
        last_price: this.data.last_price + 120 * _this.data.shoppingcardata.num
      })
    } else {
      this.setData({
        jiajipricelast:0,
        jiajiprice: 0,
        last_price: this.data.last_price - 120 * _this.data.shoppingcardata.num
      })
    }
  },
  onsaomiaobth: function (e) {
    this.setData({
      saomiaobth: e.detail,
    })
    if (e.detail) {
      this.setData({
        saomiaoprice: 30,
        last_price: this.data.last_price + 30
      })
    } else {
      this.setData({
        saomiaoprice: 0,
        last_price: this.data.last_price - 30
      })
    }
  },







  //打开地区选择遮罩层
  open_area: function (e) {
    this.setData({
      showarea: true
    })
  },
  //选择好地区并关闭地区选择器遮罩呈
  changeuserarea: function (e) {
    console.log(e)
    var str = []
    e.detail.values.forEach((item, index) => {
      if(item){
        str.push(item.name)
      }
    })
    str = str.join('--')
    this.setData({
      showarea: false,
      area: str
    })
  },
  //关闭遮罩
  closeuserarea: function () {
    this.setData({
      showarea: false
    })
  },

  //打开须知
  openkuaidipop: function () {
    this.setData({
      showkuaidipop: true
    })
  },
  openfanyipop: function () {
    this.setData({
      showfanyipop: true,
    })
  },
  openjiajipop: function () {
    this.setData({
      showjiajipop: true,
    })
  },
  opensaomiaopop: function () {
    this.setData({
      showsaomiaopop: true,
    })
  },

  closekuaidipop: function () {
    this.setData({
      showkuaidipop: false,
      showfanyipop: false,
      showjiajipop: false,
      showsaomiaopop: false,
    })
  },

  //关闭下一步遮罩
  closexiayibu: function () {
    this.setData({
      showerweima: false
    })
  }

})